import { deconstructionFn, splitMethods } from './common.js'

export const formatMethodsItem = (str) => {
  const isAsyncFunc = !!str.match(/^async /g)
  str = str.replace(/^async /, '')
  if (deconstructionFn(str, 'methods')) {
    const { name, params, body, isAsync } = deconstructionFn(str, 'methods')
    return `
      const ${name} = ${isAsyncFunc ? 'async ' : isAsync ? 'async ' : ''}(${params})=>{${body}
      }`
  }
}
export const formatMethods = (str) => {
  const methodsStr = str.replace(/methods:.*?\{/, '').replace(/\},?$/, '')
  const methodsList = splitMethods(methodsStr)
  return methodsList.map((item) => formatMethodsItem(item.trim())).join('')
}
